/*
 * Copyright 2009-2010 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.kommando.filesystem.objectsource;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.Icon;

import org.kommando.core.catalog.AbstractObjectSource;
import org.kommando.core.catalog.CachedObjectSource;
import org.kommando.core.catalog.CatalogObject;
import org.kommando.filesystem.FileSystemCatalogObjectFactory;
import org.kommando.filesystem.core.DiskObject;
import org.kommando.filesystem.core.FileObject;
import org.kommando.filesystem.core.FileSource;
import org.kommando.filesystem.core.FileSystemObject;
import org.kommando.filesystem.core.FolderObject;

/**
 * @author Peter De Bruycker
 */
public class FileSystemObjectSource extends AbstractObjectSource implements CachedObjectSource {

    private static final String ID = "filesystem:filesAndFolders";

    private List<FileSource> fileSources = new ArrayList<FileSource>();

    @Override
    public String getDescription() {
        return "Indexes files and folders";
    }

    @Override
    public Icon getIcon() {
        // TODO icon
        return null;
    }

    @Override
    public String getId() {
        return ID;
    }

    @Override
    public String getName() {
        return "Files and Folders";
    }

    @Override
    public CatalogObject getObject(String id) {
        if (id.startsWith("file:")) {
            return FileSystemCatalogObjectFactory.create(new File(id.substring(id.indexOf(":") + 1)));
        }
        return null;
    }

    /**
     * @see org.kommando.core.catalog.ObjectSource#getObjects()
     */
    @Override
    public List<CatalogObject> getObjects() {
        List<CatalogObject> fileSystemObjects = new ArrayList<CatalogObject>();

        for (FileSource fileSource : fileSources) {
            for (File file : fileSource.getFiles()) {
                fileSystemObjects.add(FileSystemCatalogObjectFactory.create(file));
            }
        }

        return fileSystemObjects;
    }

    public void addFileSource(FileSource fileSource) {
        fileSources.add(fileSource);
    }

    public void removeFileSource(FileSource fileSource) {
        fileSources.add(fileSource);
    }

    public List<FileSource> getFileSources() {
        return Collections.unmodifiableList(fileSources);
    }

    @Override
    public boolean hasChildren(CatalogObject object) {
        return object.hasExtension(FolderObject.class) || object.hasExtension(DiskObject.class);
    }

    @Override
    public List<CatalogObject> getChildren(CatalogObject object) {
        FileSystemObject fileObject = object.getExtension(FileSystemObject.class);

        List<CatalogObject> children = new ArrayList<CatalogObject>();

        for (File child : fileObject.getFile().listFiles()) {
            children.add(FileSystemCatalogObjectFactory.create(child));
        }

        return children;
    }

    // extensions: try to convert text to a FileSystemObject

    @Override
    public Class<?>[] getExtensionTypes(CatalogObject object) {
        if (!object.hasExtension(FileObject.class) && object.hasExtension(String.class)) {
            String text = object.getExtension(String.class);

            // try to convert it to a file
            if (new File(text).exists()) {
                return new Class[] { FileSystemObject.class };
            }
        }

        return null;
    }

    @Override
    public <T> T getExtension(CatalogObject object, Class<T> extensionType) {
        if (FileSystemObject.class.equals(extensionType)) {
            String file = object.getExtension(String.class);

            FileSystemObject fileObject = FileSystemCatalogObjectFactory.createFileObject(new File(file));

            return extensionType.cast(fileObject);
        }

        return null;
    }
}
